package com.bytz.modules.cms.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.modules.cms.c4c.model.C4cGetContacts;
import com.bytz.modules.cms.echop.service.IEChopService;
import com.bytz.modules.cms.order.model.OrderModel;
import com.bytz.modules.cms.order.service.IMallOrderService;
import com.bytz.modules.cms.order.service.IOrderCopyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RestController
@Api(tags = "下单相关")
@RequestMapping("/order")
public class MallOrderController {

    @Autowired
    private IMallOrderService orderService;

    @Autowired
    private IOrderCopyService orderCopyService;

    @Autowired
    private IEChopService eChopService;

    @ApiOperation("经销商订单列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<OrderModel>> page(OrderModel order,
                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                          HttpServletRequest req) {

        return Result.ok(orderService.pageByReseller(order, new Page<>(pageNo, pageSize), req));
    }

    @ApiOperation("经销商查询单条订单")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<OrderModel> queryById(@RequestParam String id) {

        return Result.ok(orderService.queryById(id));
    }

    @ApiOperation("暂存订单")
    @RequestMapping(value = "/storage", method = RequestMethod.POST)
    public Result<OrderModel> storageOrder(@RequestBody OrderModel model) {

        return Result.ok(orderService.saveOrder(model));
    }

    @ApiOperation("下单")
    @RequestMapping(value = "/placeOrder", method = RequestMethod.POST)
    public Result<OrderModel> placeOrder(@RequestBody @Valid OrderModel model) {

        return Result.ok(orderService.placeOrder(model));
    }

    @ApiOperation("上传ec材料")
    @RequestMapping(value = "/submitEcAuditFile", method = RequestMethod.POST)
    public Result<Boolean> submitEcAuditFile(@RequestBody OrderModel orderModel) {

        return Result.ok(orderService.submitEcAuditFile(orderModel));
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Boolean> delete(@RequestParam String id) {

        return Result.ok(orderService.removeById(id));
    }

    @ApiOperation("再次下单")
    @RequestMapping(value = "/copyOrder", method = RequestMethod.POST)
    public Result<OrderModel> copyOrder(@RequestParam String id) {

        return Result.ok(orderCopyService.copyOrder(id));
    }

    @ApiOperation(value = "上传盖章合同", notes = "上传盖章合同")
    @RequestMapping(value = "/uploadContract", method = RequestMethod.POST)
    public Result<Boolean> uploadContract(@RequestBody OrderModel order) {
        return Result.ok(orderService.uploadContract(order));
    }

    @ApiOperation(value = "下载合同", notes = "根据订单id下载合同")
    @RequestMapping(value = "/downloadContract", method = RequestMethod.GET)
    public Result<?> downloadContract(@RequestParam String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
        eChopService.getContractByOrderId(id, request, response);
        return Result.ok();
    }



    @ApiOperation("根据shipToId获取获取联系人")
    @RequestMapping(value = "/getContacts/{shipToId}", method = RequestMethod.GET)
    public Result<List<C4cGetContacts>> getContactsByEndUserId(@PathVariable String shipToId) {
        return Result.ok(orderService.getContactsByEndUserId(shipToId));
    }
}
